#! /bin/sh
# PCP QA Test No. 223
# Exercise the new pmns/Rebuild
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

_notrun "tests the IRIX Rebuild script"

_copy_Rebuild()
{
    scr_src=$PCP_VAR_DIR/pmns/
    cp $scr_src/Rebuild $1
    if [ -f $scr_src/Xlate-irix-names ]
    then
	cp $scr_src/Xlate-irix-names $1
    fi
}

_filter ()
{
    sed \
	-e "s/\/$$/\/PID/g" \
	-e 's/^\*\*\*.*[^*]$/*** old file/' \
	-e 's/^---.*[^-]$/--- new file/'
}

_setup() {
    PMNS_OLD=$tmp/old
    PMNS_NEW=$tmp/new
    export PMNS_OLD PMNS_NEW
    rm -rf $PMNS_OLD $PMNS_NEW
    mkdir -p $PMNS_NEW
    mkdir -p $PMNS_OLD
    echo >$PMNS_NEW/root 'root {
    foo
}
foo {
    one	0:0:1
}'
}

_setup_wxy()
{
    echo >$PMNS_NEW/root '
root {
    extra
}
extra {
    xxx
    yyy
}
#include "xxx"
#include "yyy"'

    echo >$PMNS_NEW/www.orphan '
extra.www {
    two 0:0:2
}'

    echo >$PMNS_NEW/xxx '
extra.xxx {
    two 0:0:3
}'

    echo '/* new */' >$PMNS_NEW/xxx.N
    cat $PMNS_NEW/xxx >>$PMNS_NEW/xxx.N

    echo >$PMNS_NEW/yyy '
extra.yyy {
    two 0:0:4
}'

    echo '/* old */' >$PMNS_NEW/yyy.O
    cat $PMNS_NEW/yyy >>$PMNS_NEW/yyy.O
}

_check()
{
    echo "Exit status: $1"
    type=unknown
    [ -d $PMNS_OLD ] && type=directory
    [ -f $PMNS_OLD ] && type=file
    [ -L $PMNS_OLD ] && type=symlink
    echo "PMNS_OLD pathname is: $type"
    echo "PMNS_NEW contents:"
    ls $PMNS_NEW
    $DO_VALIDATE && _validate
}

_validate()
{
    ( cd $PMNS_NEW; pmnscomp -f -n root root.bin )
    echo "pminfo:"
    pminfo -m -n $PMNS_NEW/root >$tmp.out 2>&1
    status=$?
    _filter <$tmp.out
    if [ $status != 0 ]
    then
	for file in $PMNS_NEW/*
	do
	    echo "=========================="
	    echo "new/`basename $file`"
	    echo "=========================="
	    cat $file
	done
    fi
}

_remove_rebuild()
{
    rm -f $1/Rebuild 
    if [ -f $1/Xlate-irix-names ]
    then
	rm -f $1/Xlate-irix-names
    fi
}

status=0
$sudo rm -f $tmp.* $seq.full
DO_VALIDATE=true
trap "cd $here; rm -rf $tmp $tmp.*; exit \$status" 0 1 2 3 15

# real QA test starts here

echo "=== PMNS_OLD does not exist ==="
_setup
rm -rf $PMNS_OLD
echo "=== PMNS_OLD does not exist ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== PMNS_OLD is empty ==="
_setup
echo "=== PMNS_OLD is empty ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== PMNS_OLD contains scripts that can safely be nuked ==="
_setup
touch $PMNS_OLD/Brand $PMNS_OLD/Makefile $PMNS_OLD/Rebuild
echo "=== PMNS_OLD contains scripts that can safely be nuked ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== PMNS_OLD contains files not in conflict with PMNS_NEW, copy them ==="
_setup
touch $PMNS_OLD/root.N $PMNS_OLD/urk $PMNS_OLD/urk.O
echo >$PMNS_OLD/xxx '
extra.xxx {
    two 0:0:3
}'
echo "=== PMNS_OLD contains files not in conflict with PMNS_NEW, copy them ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== files in PMNS_OLD and PMNS_NEW are identical ==="
_setup
_setup_wxy
rm $PMNS_NEW/www*
cp $PMNS_NEW/* $PMNS_OLD
echo "=== files in PMNS_OLD and PMNS_NEW are identical ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== salvage useful stuff from PMNS_OLD ==="
_setup
_setup_wxy
rm $PMNS_NEW/www*
mv $PMNS_NEW/xxx* $PMNS_OLD
DO_VALIDATE=false
_check N/A
DO_VALIDATE=true
echo "=== salvage useful stuff from PMNS_OLD ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== hard cases ==="
_setup
_setup_wxy
rm $PMNS_NEW/www*
sed -e 's/two/three/' <$PMNS_NEW/xxx.N >$PMNS_OLD/xxx
sed -e 's/two/three/' <$PMNS_NEW/yyy.O >$PMNS_OLD/yyy
_check N/A
echo "=== hard cases ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== PMNS_OLD is a file (error) ==="
_setup
rm -rf $PMNS_OLD
touch $PMNS_OLD
echo "=== PMNS_OLD is a file (error) ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== www.orphan, xxx.N and yyy.O in both places, waste the ones from PMNS_OLD ==="
_setup
_setup_wxy
touch $PMNS_OLD/www.orphan $PMNS_OLD/xxx.N $PMNS_OLD/yyy.O
_check N/A
echo "=== www.orphan, xxx.N and yyy.O in both places, waste the ones from PMNS_OLD ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== orphan (saveme) in PMNS_OLD ==="
_setup
touch $PMNS_OLD/saveme
echo "=== orphan (saveme) in PMNS_OLD ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

echo
echo "=== nested decls, a la DBMS PMDAS ==="
_setup
    echo >$PMNS_NEW/root '
root {
    extra
}
#include "extra"'

    echo >$PMNS_NEW/extra '
extra {
    xxx
    yyy
}
#include "xxx"
#include "yyy"'

    echo >$PMNS_NEW/xxx '
#define CLUST 1
#define ID xxx
extra.xxx {
#include "dummy"
#undef CLUST
#undef ID'

    echo >$PMNS_NEW/yyy '
#define CLUST 2
#define ID yyy
extra.yyy {
#include "dummy"
#undef CLUST
#undef ID'

    echo >$PMNS_NEW/dummy '
    one		0:CLUST:1
    special
}

extra.ID.special {
    two		0:CLUST:2
    three	0:CLUST:3
}
'

echo "=== nested decls, a la DBMS PMDAS ===" >>$seq.full
ls -lR $PMNS_OLD $PMNS_NEW >>$seq.full 2>&1
_copy_Rebuild $PMNS_NEW
$sudo "cd $PMNS_NEW; ./Rebuild -uv" >$tmp.err
chk=$?
_remove_rebuild $PMNS_NEW
_filter <$tmp.err
_check $chk

# all done
exit
